﻿@namespace SageKing.Studio.Pages.SageKingMessage.component

@inject SysSageKingMessageAttributeService dataService
@inject SysDictTypeService selectService
@inject IMessageService _message
@inject IDbMaintenanceService _dbMaintenanceService

@if (SelectType == null)
{
     <Spin size="SpinSize.Large" Tip="Loading..." />
}
else
{

    <Form @ref="form"
          Model="@model"
          ValidateOnChange="true"
          Layout="@FormLayout.Horizontal"
          LabelColSpan="4" WrapperColSpan="20">
        <GridRow Gutter="24">
            <GridCol Span="12">
                <FormItem Label="数据库" LabelColSpan="4">
                    <Select @bind-Value="@context.DbConfigKey"
                            DataSource="@SelectType.Children"
                            LabelName="@nameof(SysDictData.NameCodeValue)"
                            ValueName="@nameof(SysDictData.ExtData)"
                            Style="width: 100%; padding:0; margin-left:3px;"
                            TItemValue="string"
                            TItem="SysDictData"
                            Placeholder="选择数据库"
                            DefaultActiveFirstOption="false"
                            OnSelectedItemChanged="OnSelectedItemChangedHandler"
                            EnableSearch>
                    </Select>

                </FormItem>

            </GridCol>
            <GridCol Span="12">
                <FormItem Label="表">
                    <Select @bind-Value="@context.TableName"
                            DataSource="@tableOutputs"
                            LabelName="@nameof(TableOutput.TableNameDesc)"
                            ValueName="@nameof(TableOutput.TableName)"
                            Style="width: 100%;"
                            TItemValue="string"
                            TItem="TableOutput"
                            Placeholder="选择表"
                            DefaultActiveFirstOption="false"
                            OnSelectedItemChanged="OnSelectedTableChangedHandler"
                            EnableSearch>
                    </Select>
                </FormItem>

            </GridCol>
        </GridRow>
    </Form>

    <Table Class="childTable" TItem="ColumnOuput" DataSource="colOutputs" Size=@TableSize.Small EnableVirtualization PageSize="10" ScrollY="200px" PaginationPosition="bottomCenter"
           RowKey="a=>a.ColumnName" Resizable Bordered>
        <ColumnDefinitions>
            <Selection Type="@selectionType"></Selection>
            <PropertyColumn Property="c=>c.ColumnName" Title="属性名称" Sortable="true" Filterable />
            <PropertyColumn Property="c=>c.IsPrimarykey" Title="IsPrimarykey" Sortable="true" Filterable />
            <PropertyColumn Property="c=>c.DataType" Title="DB类型" Sortable="true" Filterable />
            <PropertyColumn Property="c=>c.DBToNetType" Title="Net类型" Sortable="true" Filterable />
            <PropertyColumn Property="c=>c.NetType" Title="Ice类型" Sortable="true" Filterable />
            <PropertyColumn Property="c=>c.ColumnComment" Title="描述" Sortable="true" Filterable />
        </ColumnDefinitions>
        <PaginationTemplate>
            <Pagination Class="@(context.PaginationClass + " my-custom-pagination")"
                        Total="context.Total"
                        ShowTotal="baseRender.showTotal"
                        PageSize="context.PageSize"
                        Current="context.PageIndex"
                        ShowSizeChanger
                        OnChange="context.HandlePageChange" />
        </PaginationTemplate>
    </Table>
}

@code {

    [Parameter]
    public ModelDataBaseInput model { get; set; }

    bool loading;
    AntDesign.Form<ModelDataBaseInput> form;

    BaseRenderFragment baseRender;

    //table

     SelectionType selectionType = SelectionType.Checkbox;

    //select data
    SysDictType SelectType;

    SysDictData _currDBSet;
    List<TableOutput> tableOutputs;

    public TableOutput selectTable;
    public List<ColumnOuput> colOutputs;


    protected override async Task OnInitializedAsync()
    {
        baseRender = new BaseRenderFragment();
        model = new ModelDataBaseInput();
        SelectType = await selectService.GetDetailCache(SysDictTypeConst.db_ConnectionString);
        tableOutputs = new List<TableOutput>();
    }

    public async Task<int> Save()
    {
        if (!form.Validate())
        {
            return 1;
        };

        return 0;
    }

    public void Reset()
    {
        model = new ModelDataBaseInput();
        colOutputs = new List<ColumnOuput>();
        tableOutputs = new List<TableOutput>();
        form.Reset();
    }
    private async void OnSelectedItemChangedHandler(SysDictData value)
    {
        if (value == null)
        {
            _currDBSet = null;
            return;
        }
        _currDBSet = value;
        tableOutputs = await _dbMaintenanceService.GetTableList((DBTypesEnum)value.ValueInt, value.ExtData);
        Console.WriteLine($"selected: ${value?.Name}");
    }

    private void OnSelectedTableChangedHandler(TableOutput value)
    {
        if (value == null)
        {
            selectTable = null;
            colOutputs = new List<ColumnOuput>();
            return;
        }
        selectTable = value;
        colOutputs = _dbMaintenanceService.GetColumnListByTableName(value.TableName, (DBTypesEnum)_currDBSet.ValueInt, _currDBSet.ExtData);
        Console.WriteLine($"selected: ${value?.TableName}");
    }
}
